package fr.esiea.erg5203.mvc.controllers;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import fr.esiea.erg5203.business.Address;
import fr.esiea.erg5203.business.Contact;
import fr.esiea.erg5203.persistence.dao.AbstractDAOFactory;
import fr.esiea.erg5203.persistence.dao.DAO;

@Controller  
@RequestMapping(value = "/index.jsp")
public class CreateController {	
		
	@RequestMapping(params = "action=creation_form")	
	public ModelAndView creationForm(HttpServletRequest request)
	{						
		ModelAndView result = new ModelAndView("creation_form");
		System.out.println("Create Form Meth");
		return result;
	}	
		
	@RequestMapping(params = "action=create", method = RequestMethod.POST)
	public ModelAndView create(HttpServletRequest request)
	{	
		Contact contact = null;
		Address billingAddress = null;
		Address shippingAddress = null;
		Date birthDate = null;		
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		List<Address> addresses = new ArrayList<Address>();
		
		//TODO: Regular Expression Tests before creating Contact and Adresses.
		
		try {
			birthDate = dateFormat.parse(
											(String)request.getParameter("birth_date_month") + "/" +
											(String)request.getParameter("birth_date_day") + "/" +
											(String)request.getParameter("birth_date_year")
										);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		contact = new Contact(
								(String)request.getParameter("name"),
								(String)request.getParameter("firstname"),
								(String)request.getParameter("email_user") + "@" + (String)request.getParameter("email_domain"),
								birthDate,
								true
							 );
		
		billingAddress = new Address(
										(String)request.getParameter("billing_address_number"),
										(String)request.getParameter("billing_address_street"),
										(String)request.getParameter("billing_address_zip_code"),
										(String)request.getParameter("billing_address_city")
									 );			
		
		shippingAddress  = new Address(
										(String)request.getParameter("shipping_address_number"),
										(String)request.getParameter("shipping_address_street"),
										(String)request.getParameter("shipping_address_zip_code"),
										(String)request.getParameter("shipping_address_city")
									  );
		
		addresses.add(billingAddress);
		addresses.add(shippingAddress);	
		contact.setAddresses(addresses);
		
		DAO<Contact> contactDAO = AbstractDAOFactory.getDAOFactory().getContactDAO();
		contactDAO.create(contact);
		
				
		ModelAndView result = new ModelAndView("contact_thumbnail");
		result.addObject("contact", contact);
		
		System.out.println("Create Meth");
		return result;
	}		
}
